import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
//import java.security.Security;
//import java.security.Provider;
//import javax.net.ssl.*;

/**
 * The index servlet
 *
 * @author William Li
 */

public class HttpLoginTestServlet extends HttpServlet {


    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
	return;
    }


    public void doPost(HttpServletRequest request, 
		       HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");

        Enumeration e = request.getParameterNames();
        PrintWriter out = response.getWriter();

	String page_head = request.getHeader("Referer");
	String rm_user   = request.getRemoteUser();
	String user_name = request.getParameter("user_name");
	String pass_word = request.getParameter("pass_word");
	String auth_type = request.getAuthType();
	
	out.println("page_head="+page_head);
	out.println("rm_user  ="+rm_user);
	out.println("auth_type  ="+auth_type);

	System.out.println("page_head="+page_head);
	System.out.println("rm_user  ="+rm_user);
	System.out.println("auth_type  ="+auth_type);

	PasswdFileLoader ps_file_loader = new PasswdFileLoader();
	ps_file_loader.setFileName(new File("D:/Apache2_Passwd/passwd", ".htaccess3"));
	ps_file_loader.loadFile();
	//out.println(ps_file_loader.getHashTable());

        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            String value = request.getParameter(name);
            out.println(name + " = " + value);
	}

	if((user_name+":"+pass_word).equals(decodeAuthorization(
	    (encodeAuthorization(user_name, pass_word))))) {
	    out.println("<BR>User login OK!");
	    out.println("<BR>1="+ (encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>2="+ (encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>3="+ (encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>4="+ (encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>User login OK!");
	    out.println("<BR>decodeAuth1="+decodeAuthorization(encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>decodeAuth2="+decodeAuthorization(encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>decodeAuth3="+decodeAuthorization(encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>decodeAuth4="+decodeAuthorization("d2xpOmxpMDU3OA=="));
	    out.println("<BR>decodeAuth5="+decodeAuthorization("d2xpOmxpMDU3OA== "));
	}
	else {
	    out.println("<BR>User login No OK!");
	    out.println("<BR>encodeAuth="+(encodeAuthorization(user_name, pass_word)));
	    out.println("<BR>decodeAuth="+decodeAuthorization(encodeAuthorization(user_name, pass_word)));
	}

	/*
	RequestDispatcher rd = new RequestDispatcher();
	rd.forward(HttpServletRequest request, 
		       HttpServletResponse response));
	*/
    }

    private String encodeAuthorization (String username, String passwd) {

	String authorization = username + ":" + passwd;
	sun.misc.BASE64Encoder enCoder = new sun.misc.BASE64Encoder();
	//return "Basic " + enCoder.encode(authorization.getBytes());
	return enCoder.encode(authorization.getBytes());
    }

    private String decodeAuthorization (String encode_user_pass) {

	String decode_output ="";

	sun.misc.BASE64Decoder deCoder = new sun.misc.BASE64Decoder();

	try {
	    decode_output = new String (deCoder.decodeBuffer(encode_user_pass));
	}
	catch (IOException e){
	}
	return decode_output;
    }

    /*
    private String pageHeadCheck (String page_head) {
	String auth = req.getHeader( "Authorization" );

	if( !allowUser( auth )) {
	    res.setHeader( "WWW-Authenticate", "BASIC realm=\"registered_users@localhost\"" );
	    res.sendError( res.SC_UNAUTHORIZED );
	}
	else {
	    req.getRemoteUser();
	    out.println( "Top Secret Stuff" );
	}
    }

    protected boolean allowUser( String auth ) throws IOException {
	if( auth == null )
	    return false;

	if( !auth.toUpperCase().startsWith("BASIC "))
	    return false;

	String userpassEncoded = auth.substring(6);

	sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
	String userpassDecoded = new String( dec.decodeBuffer( userpassEncoded ));


	if( "allowed".equals( getUsersTable().get( userpassDecoded ))) {
	    return true;
	}
	else {
	    return false;
	}
    }
    */
}






